import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { FactoryToyItem, FactoryToyItemId } from './FactoryToyItem';

export interface UserToyItemAttributes {
  id: string;
  userId: string;
  toyId: string;
  createdTime: Date;
  updatedTime: Date;
}

export type UserToyItemPk = 'id';
export type UserToyItemId = UserToyItem[UserToyItemPk];
export type UserToyItemOptionalAttributes = 'createdTime' | 'updatedTime';
export type UserToyItemCreationAttributes = Optional<
  UserToyItemAttributes,
  UserToyItemOptionalAttributes
>;

export class UserToyItem
  extends Model<UserToyItemAttributes, UserToyItemCreationAttributes>
  implements UserToyItemAttributes
{
  id!: string;
  userId!: string;
  toyId!: string;
  createdTime!: Date;
  updatedTime!: Date;

  // UserToyItem belongsTo FactoryToyItem via toyId
  toy!: FactoryToyItem;
  getToy!: Sequelize.BelongsToGetAssociationMixin<FactoryToyItem>;
  setToy!: Sequelize.BelongsToSetAssociationMixin<
    FactoryToyItem,
    FactoryToyItemId
  >;
  createToy!: Sequelize.BelongsToCreateAssociationMixin<FactoryToyItem>;

  static initModel(sequelize: Sequelize.Sequelize): typeof UserToyItem {
    return UserToyItem.init(
      {
        id: {
          type: DataTypes.CHAR(36),
          allowNull: false,
          primaryKey: true,
          comment: '雪花主键',
        },
        userId: {
          type: DataTypes.CHAR(36),
          allowNull: false,
          field: 'user_id',
        },
        toyId: {
          type: DataTypes.CHAR(36),
          allowNull: false,
          comment: '所属玩具ID',
          references: {
            model: 'factory_toy_item',
            key: 'id',
          },
          field: 'toy_id',
        },
        createdTime: {
          type: DataTypes.DATE,
          allowNull: false,
          defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
          comment: '创建时间',
          field: 'created_time',
        },
        updatedTime: {
          type: DataTypes.DATE,
          allowNull: false,
          defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
          comment: '更新时间',
          field: 'updated_time',
        },
      },
      {
        sequelize,
        tableName: 'user_toy_item',
        timestamps: false,
        indexes: [
          {
            name: 'PRIMARY',
            unique: true,
            using: 'BTREE',
            fields: [{ name: 'id' }],
          },
          {
            name: 'idx_user_toy_item',
            using: 'BTREE',
            fields: [{ name: 'id' }],
          },
          {
            name: 'fk_user_toy_item',
            using: 'BTREE',
            fields: [{ name: 'toy_id' }],
          },
        ],
      },
    );
  }
}
